//
//  iCarouselViewController.m
//  wuliangye
//
//  Created by yeeyuntech on 16/10/27.
//  Copyright © 2016年 jarze. All rights reserved.
//

#import "iCarouselViewController.h"
#import "iCarousel.h"
#import "UIImageView+WebCache.h"
#import "GCD.h"
#import "UIView+SetRect.h"

@interface iCarouselViewController ()<iCarouselDataSource, iCarouselDelegate,UIActionSheetDelegate>



@property (nonatomic, assign) BOOL wrap;
@property (nonatomic, strong) NSMutableArray *picturesData;



@property (nonatomic, strong) iCarousel      *carousel;   // iCarousel


@end

@implementation iCarouselViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
    [self setNav];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)setup {
    
    
    // https://github.com/nicklockwood/iCarousel
    
    self.view.layer.masksToBounds = YES;
    self.view.backgroundColor  = [UIColor whiteColor];
    
    self.carousel            = [[iCarousel alloc] initWithFrame:self.view.bounds];
    self.carousel.type       = iCarouselTypeWheel;
    self.carousel.delegate   = self;
    self.carousel.dataSource = self;
    [self.view addSubview:_carousel];
    
    
    // 初始化数据源
    _picturesData = @[@"http://pic.cnitblog.com/avatar/607542/20140226182241.png",
                           @"http://pic.cnitblog.com/avatar/751954/20150430140947.png",
                           @"http://pic.cnitblog.com/avatar/680363/20150315172929.png",
                           @"http://pic.cnitblog.com/avatar/490617/20140612182827.png",
                           @"http://pic.cnitblog.com/avatar/77734/20140925202137.png",
                           @"http://pic.cnitblog.com/avatar/726558/20150302212034.png",
                           @"http://pic.cnitblog.com/avatar/619104/20140622195607.png",
                           @"http://pic.cnitblog.com/avatar/695863/20150416154015.png",
                           @"http://pic.cnitblog.com/avatar/526391/20141226102235.png",
                           @"http://pic.cnitblog.com/avatar/485855/20140824172432.png",
                           @"http://pic.cnitblog.com/avatar/741774/20150408141002.png",
                      @"http://pic.cnitblog.com/avatar/618574/20140329230350.png",@"http://img.wdjimg.com/image/video/d999011124c9ed55c2dd74e0ccee36ea_0_0.jpeg",
                      @"http://img.wdjimg.com/image/video/2ddcad6dcc38c5ca88614b7c5543199a_0_0.jpeg",
                      @"http://img.wdjimg.com/image/video/6d6ccfd79ee1deac2585150f40915c09_0_0.jpeg",
                      @"http://img.wdjimg.com/image/video/2111a863ea34825012b0c5c9dec71843_0_0.jpeg",
                      @"http://img.wdjimg.com/image/video/b4085a983cedd8a8b1e83ba2bd8ecdd8_0_0.jpeg",
                      @"http://img.wdjimg.com/image/video/2d59165e816151350a2b683b656a270a_0_0.jpeg",
                      @"http://img.wdjimg.com/image/video/dc2009ee59998039f795fbc7ac2f831f_0_0.jpeg"].mutableCopy;
    [self.carousel reloadData];
    self.carousel.currentItemIndex = 3;
    
}

#pragma mark - iCarousel's delegate.

- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    
    return self.picturesData.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view {
    
    if (view == nil) {
        
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 250, 400)];
    }
    
    UIImageView *pointView = (UIImageView *)view;
    pointView.contentMode  = UIViewContentModeScaleAspectFit;
    
//    WaterfallPictureModel *model = self.picturesData.data.blogs[index];
    
    [pointView sd_setImageWithURL:[NSURL URLWithString:self.picturesData[index]]
                        completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            pointView.image = image;
                        }];
    
    return view;
}


- (void)setNav{
    UIBarButtonItem *b1 = [[UIBarButtonItem alloc] initWithTitle:@"switch type" style:UIBarButtonItemStylePlain target:self action:@selector(switchCarouselType)];
    
    UIBarButtonItem *b2 = [[UIBarButtonItem alloc] initWithTitle:@"vertical" style:UIBarButtonItemStylePlain target:self action:@selector(toggleOrientation)];
    UIBarButtonItem *b3 = [[UIBarButtonItem alloc] initWithTitle:@"wrap" style:UIBarButtonItemStylePlain target:self action:@selector(toggleWrap)];
    
    self.navigationItem.rightBarButtonItems = @[b1,b2,b3];
}

- (void)switchCarouselType
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Select Carousel Type"
                                                       delegate:self
                                              cancelButtonTitle:nil
                                         destructiveButtonTitle:nil
                                              otherButtonTitles:@"Linear", @"Rotary", @"Inverted Rotary", @"Cylinder", @"Inverted Cylinder", @"Wheel", @"Inverted Wheel", @"CoverFlow", @"CoverFlow2", @"Time Machine", @"Inverted Time Machine", @"Custom", nil];
    [sheet showInView:self.view];
}

- (void)toggleOrientation
{
    //carousel orientation can be animated
    [UIView beginAnimations:nil context:nil];
    self.carousel.vertical = !self.carousel.vertical;
    [UIView commitAnimations];
    
    //update button
    [self.navigationItem.rightBarButtonItems objectAtIndex:1].title = self.carousel.vertical? @"Vertical": @"Horizontal";
}

- (void)toggleWrap
{
    self.wrap = !self.wrap;
    [self.navigationItem.rightBarButtonItems objectAtIndex:2].title = self.wrap? @"Wrap: ON": @"Wrap: OFF";
    [self.carousel reloadData];
}


#warning *******************************************
- (IBAction)updateViewpointOffset:(UISlider *)slider
{
    CGSize offset = CGSizeMake(0.0f, slider.value);
    _carousel.viewpointOffset = offset;
}

- (IBAction)updateContentOffset:(UISlider *)slider
{
    CGSize offset = CGSizeMake(0.0f, slider.value);
    _carousel.contentOffset = offset;
}
- (IBAction)insertItem
{
    NSInteger index = MAX(0, self.carousel.currentItemIndex);
    [self.picturesData insertObject:[self.picturesData lastObject] atIndex:(NSUInteger)index];
    [self.carousel insertItemAtIndex:index animated:YES];
}

- (IBAction)removeItem
{
    if (self.carousel.numberOfItems > 0)
    {
        NSInteger index = self.carousel.currentItemIndex;
        [self.picturesData removeObjectAtIndex:(NSUInteger)index];
        [self.carousel removeItemAtIndex:index animated:YES];
    }
}


#pragma mark -
#pragma mark UIActionSheet methods

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex >= 0)
    {
        //map button index to carousel type
        iCarouselType type = buttonIndex;
        
        //carousel can smoothly animate between types
        [UIView beginAnimations:nil context:nil];
        self.carousel.type = type;
        [UIView commitAnimations];
        
        //update title
        self.navigationItem.title = [actionSheet buttonTitleAtIndex:buttonIndex];
    }
}


- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0, 0.0, 1.0, 0.0);
    return CATransform3DTranslate(transform, 0.0, 0.0, offset * self.carousel.itemWidth);
}

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value;
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return self.wrap;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value * 1.05;
        }
        case iCarouselOptionFadeMax:
        {
            if (self.carousel.type == iCarouselTypeCustom)
            {
                //set opacity based on distance from camera
                return 0.0;
            }
            return value;
        }
        case iCarouselOptionShowBackfaces:
        case iCarouselOptionRadius:
        case iCarouselOptionAngle:
        case iCarouselOptionArc:
        case iCarouselOptionTilt:
        case iCarouselOptionCount:
        case iCarouselOptionFadeMin:
        case iCarouselOptionFadeMinAlpha:
        case iCarouselOptionFadeRange:
        case iCarouselOptionOffsetMultiplier:
        case iCarouselOptionVisibleItems:
        {
            return value;
        }
    }
}

#pragma mark -
#pragma mark iCarousel taps

- (void)carousel:(__unused iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSString *item = (self.picturesData)[(NSUInteger)index];
    NSLog(@"Tapped view number: %@", item);
}

- (void)carouselCurrentItemIndexDidChange:(__unused iCarousel *)carousel
{
    NSLog(@"Index: %@", @(self.carousel.currentItemIndex));
}

- (void)dealloc
{
    //it's a good idea to set these to nil here to avoid
    //sending messages to a deallocated viewcontroller
    _carousel.delegate = nil;
    _carousel.dataSource = nil;
}
@end
